package tv.youxing.android.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import java.util.ArrayList;
import java.util.List;

import tv.youxing.android.R;
import tv.youxing.android.custom.JazzyViewPager;
import tv.youxing.android.custom.OutlineContainer;


/**
 * 发现图片 图片点击放大
 */
public class MaxImgDetailsActivity extends AppCompatActivity {



    private JazzyViewPager mViewPager;
    private TextView txt_index;
    private List<ImageView> mImageViews = new ArrayList<ImageView>();
    private DisplayImageOptions options;
    private String mListImg[];
    private int PicIndex = 0;




    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_test2);
        mViewPager = (JazzyViewPager) findViewById(R.id.pager);
        txt_index = (TextView)findViewById(R.id.txt_index);
        Intent tent = getIntent();
        mListImg = tent.getStringArrayExtra("filePath");







        for (int i = 0; i < mListImg.length; i++) {
            System.out.println(mListImg[i]);
        }

        PicIndex = tent.getIntExtra("PicIndex", 0);
        //PicIndex = PicIndex+1;
        options = new DisplayImageOptions.Builder()	// 设置图片下载期间显示的图片
                .showImageForEmptyUri(R.drawable.no_photo)	// 设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.drawable.no_photo)		// 设置图片加载或解码过程中发生错误显示的图片
                .cacheInMemory(true)						// 设置下载的图片是否缓存在内存中
                .cacheOnDisc(true)							// 设置下载的图片是否缓存在SD卡中
                .displayer(new RoundedBitmapDisplayer(5))	// 设置成圆角图片
                .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
                .build();
        if(mListImg != null)
        {
            init();
            txt_index.setText(""+(PicIndex+1)+"/"+mListImg.length);
        }
    }



    private void init()
    {


        for (int i = 0; i < mListImg.length; i++) {
            ImageView imageView = new ImageView(this);
            ImageLoader.getInstance().displayImage(mListImg[i],imageView,options);
            mImageViews.add(imageView);
        }
        mViewPager.setTransitionEffect(JazzyViewPager.TransitionEffect.Standard);
        mViewPager.setAdapter(new MyAdapter());
        mViewPager.setCurrentItem(PicIndex);
        mViewPager.setOnPageChangeListener(new MyPageChangeListener());
        mViewPager.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                if (mImageViews.size() == 0 || mImageViews.size() == 1)
                    return true;
                else
                    return false;
            }
        });
    }


    private class MyPageChangeListener implements ViewPager.OnPageChangeListener {

        /**
         * This method will be invoked when a new page becomes selected.
         * position: Position index of the new selected page.
         */
        public void onPageSelected(int position) {

            if ( mImageViews.size() > 1) { //多于1，才会循环跳转
                mViewPager.setCurrentItem(position, false);
            }
            txt_index.setText(""+(position+1)+"/"+mImageViews.size());
        }

        public void onPageScrollStateChanged(int arg0) {
        }

        public void onPageScrolled(int arg0, float arg1, int arg2) {
            int index = mViewPager.getCurrentItem();

        }
    }
    private class MyAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return mListImg.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object obj) {
            if (view instanceof OutlineContainer) {
                return ((OutlineContainer) view).getChildAt(0) == obj;
            } else {
                return view == obj;
            }
        }

        @Override
        public void destroyItem(View container, int position, Object object) {
            ((ViewPager) container).removeView(mViewPager
                    .findViewFromObject(position));
        }

        @Override
        public Object instantiateItem(View container, int position)
        {

            ((ViewPager) container).addView(mImageViews.get(position), 0);
            mViewPager.setObjectForPosition(mImageViews.get(position), position);
            mImageViews.get(position).setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    MaxImgDetailsActivity.this.finish();
                }
            });
            return mImageViews.get(position);
        }
    }


}
